package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.pojo.User;
import com.guigu.www.pojo.Users;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.service.UsersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    UsersService service;
    @RequestMapping("queryusers.action")
    public PageVo<Users> queryusers(Users users,
                                    @RequestParam(value = "pagenum",defaultValue = "1")int pagenum,
                                    @RequestParam(value = "pagesize",defaultValue = "5")int pagesize){
        QueryWrapper queryWrapper=new QueryWrapper();
        if(StringUtils.isNotBlank(users.getLoginid())){
            queryWrapper.like("name",users.getLoginid());
        }
        Page<Users> page=service.page(new Page<Users>(pagenum,pagesize),queryWrapper);
        PageVo<Users> pageVo=new PageVo<>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }
    @RequestMapping("addusers.action")
    public Map addusers(Users users, MultipartFile photopathh, HttpServletRequest request) throws IOException {
        String photopath=photopathh.getOriginalFilename();
        String path=request.getServletContext().getRealPath("/upload");
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        //保存
        photopathh.transferTo(new File(path,photopath));
        //保存的路劲存入数据库
        users.setPhotopath("upload/"+photopath);
        Map map=new HashMap();
        boolean res=service.save(users);
        if(res){
            map.put("msg","添加成功");
            map.put("code","1");
        }else {
            map.put("msg","添加失败");
            map.put("code","0");
        }
        return map;
    }

    @RequestMapping("deleteusers.action/{id}")
    public Map deleteusers(@PathVariable int id){
        Map map=new HashMap();
        boolean res=service.removeById(id);
        if(res){
            map.put("msg","删除成功");
            map.put("code","1");
        }else {
            map.put("msg","删除失败");
            map.put("code","0");
        }
        return map;
    }
    @RequestMapping("updateusers.action")
    public Map updateusers(Users users, MultipartFile photopathh, HttpServletRequest request) throws IOException {
        String photopath=photopathh.getOriginalFilename();
        String path=request.getServletContext().getRealPath("/upload");
        File file=new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        //保存
        photopathh.transferTo(new File(path,photopath));
        //保存的路劲存入数据库
        users.setPhotopath("upload/"+photopath);
        Map map=new HashMap();
        boolean res=service.updateById(users);
        if(res){
            map.put("msg","修改成功");
            map.put("code","1");
        }else {
            map.put("msg","修改失败");
            map.put("code","0");
        }
        return map;
    }
    @RequestMapping("querybyusersid.action/{id}")
    public Users querybyusersid(@PathVariable int  id){
        Users res=service.getById(id);
        return res;
    }

    //根据当前角色查询
    @RequestMapping("/queryimg.action")
    public Users queryimg(String username){

        return service.queryimg(username);
    }
}
